package com.diudiutechnology.www.yadiudiu.util.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.diudiutechnology.www.yadiudiu.R;

/**
 * Created by Administrator on 2017/9/5.
 */

public class SelectPhotoPop extends PopupWindow {

    @SuppressWarnings("deprecation")
    public SelectPhotoPop(Context activity, View.OnClickListener itemsOnClick) {
        /**
         * popWindow
         */
        final View shareView = View.inflate(activity,
                R.layout.jobwanted_for_camera, null);
        shareView.findViewById(R.id.item_popupwindows_camera)
                .setOnClickListener(itemsOnClick);
        shareView.findViewById(R.id.item_popupwindows_Photo)
                .setOnClickListener(itemsOnClick);
        shareView.findViewById(R.id.item_popupwindows_cancel)
                .setOnClickListener(itemsOnClick);

        this.setContentView(shareView);

        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);

        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        this.setFocusable(true);

        ColorDrawable dw = new ColorDrawable(0x60000000);

        this.setAnimationStyle(R.style.AnimBottom);

        this.setBackgroundDrawable(dw);

        shareView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = shareView.findViewById(R.id.parent).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    @SuppressWarnings("deprecation")
    // 获取指定Activity的截屏，保存到png文件
    public static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }

}
